package cn.djp.canting.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;


import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import cn.djp.canting.R;
import cn.djp.canting.adapter.SettingItemAdapter;
import cn.djp.canting.bean.Setting;

public class SettingActivity extends AppCompatActivity {

    private Toolbar setting_toolbar;
    private ListView setting_listview;
    private final Context context = SettingActivity.this;
    private AlertDialog alertDialog;
    private Button exit;
    private Button cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView() {
        setting_toolbar = (Toolbar) findViewById(R.id.setting_toolbar);
        setting_listview = (ListView) findViewById(R.id.setting_listview);

        setSupportActionBar(setting_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setting_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        createExitDialog();
        updateUi();

        //给设置列表的item设置点击事件
        setting_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    //修改密码
                    case 0:
                        Intent intent =getIntent();
                        //获取到用户手机号并传递给下一个活动
                        String phone = intent.getStringExtra("phone");
                        Intent intent1 = new Intent(context,PasswordActivity.class);
                        intent1.putExtra("phone",phone);
                        //跳转至修改密码
                        startActivity(intent1);
                        break;

                    //关于界面
                    case 1:
                        startActivity(new Intent(context,InfoActivity.class));
                        break;

                    //退出登录
                    case 2:
                        //点击"退出登录"项时,显示自定义对话框
                        alertDialog.show();
                        break;
                }
            }
        });
    }

    //创建自定义对话框
    private void createExitDialog() {
        View view = LayoutInflater.from(context).inflate(
                R.layout.exit_dialog, null, false
        );
        alertDialog = new AlertDialog.Builder(context).setView(view).create();
        exit = (Button) view.findViewById(R.id.exit_yes);
        cancel = (Button) view.findViewById(R.id.exit_no);

        //点击"取消"按钮时,仅关闭对话框无其他动作
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        //点击"退出"按钮时,用户退出至登录界面
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, LoginActivity.class));
                finish();
            }
        });
    }

    //更新设置界面
    private void updateUi() {
        List<Setting> settings = new ArrayList<Setting>();
        Setting setting = new Setting("修改密码");
        settings.add(setting);
        setting = new Setting("关于");
        settings.add(setting);
        setting = new Setting("退出登录");
        settings.add(setting);
        setting_listview.setAdapter(new SettingItemAdapter(this, settings));
    }
}
